﻿using System;
using System.Collections.Generic;
using System.Linq;
using TradeMeSDK;

namespace TradeMo
{
    public class CriteriaPropertyFlatmateViewModel : BaseViewModel
    {
        private SearchResidentialCriteria _criteria;
        public SearchResidentialCriteria Criteria
        {
            get
            {
                if (_criteria == null)
                {
                    _criteria = new SearchResidentialCriteria();
                    UpdateFields();
                }
                return _criteria;
            }
            set
            {
                _criteria = value;
                UpdateFields();
            }
        }

        ////////////////////////////////////////////////////////////////////////////

        private Locality _selectedRegion;
        public Locality SelectedRegion
        {
            get { return _selectedRegion; }
            set
            {
                _selectedRegion = value;
                NotifyPropertyChanged("RegionName");
                SelectedDistrict = null;
                Criteria.Region = _selectedRegion != null ? _selectedRegion.LocalityId : 0;
            }
        }

        public string RegionName
        {
            get
            {
                return SelectedRegion == null ? "New Zealand" : SelectedRegion.Name;
            }
        }

        public List<SelectorItem<Locality>> Regions
        {
            get
            {
                if (!App.TradeMe.Catalog.IsDownloaded) return null;

                var list = new List<SelectorItem<Locality>>();
                list.Add(new SelectorItem<Locality> { Title = "New Zealand" });
                list.AddRange(
                    App.TradeMe.Catalog.Localities.Select(area => new SelectorItem<Locality> { Title = area.Name, Item = area }));
                return list.ToList();
            }
        }

        ///////////////////////////////////////////////////////////////////////////////

        private District _selectedDistrict;
        public District SelectedDistrict
        {
            get { return _selectedDistrict; }
            set
            {
                _selectedDistrict = value;
                NotifyPropertyChanged("DistrictName");
                NotifyPropertyChanged("DistrictVisible");
                SelectedSuburb = null;
                Criteria.District = _selectedDistrict != null ? _selectedDistrict.DistrictId : 0;
            }
        }


        public bool DistrictVisible
        {
            get
            {
                if (SelectedRegion == null) return false;
                if (SelectedRegion.Districts == null) return false;
                return SelectedRegion.Districts.Length > 0;
            }
        }

        public string DistrictName
        {
            get
            {
                return SelectedDistrict == null ? "All districts" : SelectedDistrict.Name;
            }
        }

        public List<SelectorItem<District>> Districts
        {
            get
            {
                if (!App.TradeMe.Catalog.IsDownloaded) return null;

                if (!DistrictVisible) return null;
                var list = new List<SelectorItem<District>>();
                list.Add(new SelectorItem<District> { Title = "All districts" });
                list.AddRange(
                    SelectedRegion.Districts.Select(
                        district => new SelectorItem<District> { Title = district.Name, Item = district }));
                return list.ToList();
            }
        }

        ///////////////////////////////////////////////////////////////////////////////

        private Suburb _selectedSuburb;
        public Suburb SelectedSuburb
        {
            get { return _selectedSuburb; }
            set
            {
                _selectedSuburb = value;
                NotifyPropertyChanged("SuburbName");
                NotifyPropertyChanged("SuburbVisible");
                NotifyPropertyChanged("SearchNearbyVisible");
                Criteria.Suburb = _selectedSuburb != null ? _selectedSuburb.SuburbId : 0;
            }
        }


        public bool SuburbVisible
        {
            get
            {
                if (SelectedDistrict == null) return false;
                if (SelectedDistrict.Suburbs == null) return false;
                return SelectedDistrict.Suburbs.Length > 0;
            }
        }

        public bool SearchNearbyVisible
        {
            get { return SelectedSuburb != null; }
        }

        public string SuburbName
        {
            get
            {
                return SelectedSuburb == null ? "All suburbs" : SelectedSuburb.Name;
            }
        }

        public List<SelectorItem<Suburb>> Suburbs
        {
            get
            {
                if (!App.TradeMe.Catalog.IsDownloaded) return null;

                if (!SuburbVisible) return null;
                var list = new List<SelectorItem<Suburb>>();
                list.Add(new SelectorItem<Suburb> { Title = "All suburbs" });
                list.AddRange(
                    SelectedDistrict.Suburbs.Select(
                        suburb => new SelectorItem<Suburb> { Title = suburb.Name, Item = suburb }));
                return list.ToList();
            }
        }


        /////////////////////////////////////////////////////////////////////

        private SelectorItem<string> _selectedPriceFrom;
        public SelectorItem<string> SelectedPriceFrom
        {
            get
            {
                return _selectedPriceFrom ?? (_selectedPriceFrom = new SelectorItem<string> { Title = "Any" });
            }
            set
            {
                _selectedPriceFrom = value;
                NotifyPropertyChanged("PriceFromName");
                Criteria.MinPrice = _selectedPriceFrom.Item != null ? Convert.ToInt32(_selectedPriceFrom.Item) : 0;
            }
        }
        public string PriceFromName
        {
            get { return SelectedPriceFrom.Title; }
            set { SelectedPriceFrom.Title = value; }
        }

        private SelectorItem<string> _selectedPriceTo;
        public SelectorItem<string> SelectedPriceTo
        {
            get
            {
                return _selectedPriceTo ?? (_selectedPriceTo = new SelectorItem<string> { Title = "Any" });
            }
            set
            {
                _selectedPriceTo = value;
                NotifyPropertyChanged("PriceToName");
                Criteria.MaxPrice = _selectedPriceTo != null ? Convert.ToInt32(_selectedPriceTo.Item) : 0;
            }
        }
        public string PriceToName
        {
            get { return SelectedPriceTo.Title; }
            set { SelectedPriceTo.Title = value; }
        }

        public List<SelectorItem<string>> PriceItems
        {
            get
            {
                var list = new List<SelectorItem<string>>
                    {
                        new SelectorItem<string> {Item="0", Title = "Any"},
                        new SelectorItem<string> {Item="50", Title = "$50"},
                        new SelectorItem<string> {Item="100", Title = "$100"},
                        new SelectorItem<string> {Item="150", Title = "$150"},
                        new SelectorItem<string> {Item="200", Title = "$200"},
                        new SelectorItem<string> {Item="250", Title = "$250"},
                        new SelectorItem<string> {Item="300", Title = "$300"},
                        new SelectorItem<string> {Item="350", Title = "$350"},
                        new SelectorItem<string> {Item="400", Title = "$400"},
                        new SelectorItem<string> {Item="500", Title = "$500"},
                        new SelectorItem<string> {Item="600", Title = "$600"},
                        new SelectorItem<string> {Item="800", Title = "$800"},
                        new SelectorItem<string> {Item="1000", Title = "$1000+"},
                    };

                return list;
            }
        }


        ////////////////////////////////////////////////////////////////////////////

        private SelectorItem<string> _selectedSortBy;
        public SelectorItem<string> SelectedSortBy
        {
            get
            {
                return _selectedSortBy ?? (_selectedSortBy = new SelectorItem<string> { Title = "Default" });
            }
            set
            {
                _selectedSortBy = value;
                NotifyPropertyChanged("SortByName");
                Criteria.SortOrder = _selectedSortBy != null ? _selectedSortBy.Item : null;
            }
        }
        public string SortByName
        {
            get { return SelectedSortBy.Title; }
            set { SelectedSortBy.Title = value; }
        }

        public string SortByItem
        {
            get { return Criteria.SortOrder; }
            set { Criteria.SortOrder = value; }
        }

        public List<SelectorItem<string>> SortByItems
        {
            get
            {
                List<SelectorItem<string>> list = new List<SelectorItem<string>>
                    {
                        new SelectorItem<string> {Title = "Default", Item = "Default"},
                        new SelectorItem<string> {Title = "Lowest Price", Item = "PriceAsc"},
                        new SelectorItem<string> {Title = "Highest Price", Item = "PriceDesc"},
                        new SelectorItem<string> {Title = "Latest Listings", Item = "ExpiryDesc"},
                    };

                return list;
            }
        }

        /////////////////////////////////////////////////////////////////////

        public void UpdateFields()
        {
            int district = Criteria.District;
            int suburb = Criteria.Suburb;
            if (Criteria.Region != 0)
            {
                SelectedRegion = App.TradeMe.Catalog.Localities.FirstOrDefault(l => l.LocalityId == Criteria.Region);
                if (district != 0)
                {
                    SelectedDistrict = SelectedRegion.Districts.FirstOrDefault(d => d.DistrictId == district);
                    if (suburb != 0)
                        SelectedSuburb = SelectedDistrict.Suburbs.FirstOrDefault(s => s.SuburbId == suburb);
                }
            }

            if (!string.IsNullOrEmpty(Criteria.SortOrder))
                SelectedSortBy = SortByItems.FirstOrDefault(s => s.Item == Criteria.SortOrder);
        }

        public void Reset()
        {
            SelectedRegion = null;
            SelectedPriceFrom = null;
            SelectedPriceTo = null;
            SelectedSortBy = null;
            UpdateFields();
        }

    }
}
